package edu.compila.client;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.Widget;

import edu.compila.client.api.Tela;
import edu.compila.client.compila.Lexico;
import edu.compila.client.compila.TabelaSimbolos;
import edu.compila.client.compila.Token;

public class TesteTela extends Composite {
	
	@SuppressWarnings("unchecked")
	public void testar(){
		limpar();
		TabelaSimbolos tabela = new TabelaSimbolos();
		Lexico lexico = new Lexico(fonte.getText(),tabela);
		
		String semEspacos = null;
		semEspacos = lexico.retirarEspacos();
		if(semEspacos != null){
			destino.setText("\n###Etapa de retirar espacos..###\n" 
					+ semEspacos 
					+       "\n################################\n");
			espacoImg.setUrl("on.png");
		} else {
			espacoImg.setUrl("erro.png");
		}
		
		String semComentarios = null;
		semComentarios = lexico.retirarComentarios();
		if(semComentarios != null){
			destino.setText(destino.getText() + "\n### Etapa de retirar comentarios...###\n" 
					+ semComentarios 
					+                           "\n######################################\n");
			comentarioImg.setUrl("on.png");
		} else {
			comentarioImg.setUrl("erro.png");

		}
		destino.setText(destino.getText() + "\n### Tokens identificados ###\n");
		
		List<Token> tokens = new ArrayList<Token>();
		while(lexico.hasNext()) {
			Token t = lexico.nextToken();
			if(t != null) {
				tokens.add(t);
			}
		}
		String tokensString ="";
		for (Iterator iterator = tokens.iterator(); iterator.hasNext();) {
			Token token = (Token) iterator.next();
			tokensString += "Lexema: " 
					+ token.getLexema()
					+ " | Padrao: " 
					+ token.getPadrao().getNome()
					+ "\n";
		}
		destino.setText(destino.getText() + tokensString);
		destino.setText(destino.getText() +"\n############################\n");
		if(tokens.isEmpty()) {
			tokensImg.setUrl("erro.png");
		} else {
			tokensImg.setUrl("on.png");
		}
	}


	
	private static final String TOKEN = "teste";

	public static Tela init() {
		return new Tela(TOKEN) {
			@Override
			protected Widget createWidget() {
				return new TesteTela();
			}
		};
	}
	private DockPanel dockPanel;
	
	private Caixa caixa;
	private Caixa caixa_1;
	private FlowPanel flowPanel;
	private FlowPanel flowPanel_1;
	private FlexTable flexTable;
	private FlexTable flexTable_1;
	private Label lblCdigoOriginal;
	private TextArea fonte;
	private Label lblCdigoCompilado;
	private TextArea destino;
	private FlexTable flexTable_2;
	private Image espacoImg;
	private Image comentarioImg;
	private Image tokensImg;
	private Label lblEspaosEmBranco;
	private Label lblComentrios;
	private Label lblExtrairTokens;
	private FlexTable flexTable_4;
	private Image image;
	private Image image_1;
	private Image image_2;
	
	
	public TesteTela() {
		createContents();
	}
	
	private void createContents() {
		{
			dockPanel = new DockPanel();
			dockPanel.setStyleName("gwt-MainPanel");
			initWidget(dockPanel);
			dockPanel.setSize("100%", "100%");
		}
		{
			flexTable_4 = new FlexTable();
			dockPanel.add(flexTable_4, DockPanel.NORTH);
		}
		{
			caixa_1 = new Caixa();
			dockPanel.add(caixa_1, DockPanel.WEST);
			dockPanel.setCellHeight(caixa_1, "100%");
			dockPanel.setCellWidth(caixa_1, "50%");
		}
		{
			caixa = new Caixa();
			dockPanel.add(caixa, DockPanel.EAST);
			dockPanel.setCellHeight(caixa, "100%");
			dockPanel.setCellWidth(caixa, "50%");
		}
		{
			flowPanel = new FlowPanel();
			caixa_1.getMain().setWidget(flowPanel);
			flowPanel.setSize("100%", "100%");
		}
		{
			flowPanel_1 = new FlowPanel();
			caixa.getMain().setWidget(flowPanel_1);
			flowPanel_1.setSize("100%", "100%");
		}
		{
			flexTable = new FlexTable();
			flowPanel.add(flexTable);
			flexTable.setWidth("100%");
		}
		{
			flexTable_1 = new FlexTable();
			flowPanel_1.add(flexTable_1);
			flexTable_1.setWidth("100%");
		}
		{
			lblCdigoOriginal = new Label("Código Original");
			lblCdigoOriginal.setWordWrap(false);
			lblCdigoOriginal.setStyleName("gwt-MainPanel-Titulo");
			flexTable.setWidget(0, 0, lblCdigoOriginal);
		}
		{
			fonte = new TextArea();
			fonte.setStyleName("gwt-TesteTela-TextArea");
			fonte.setText("#- Código de teste -#\r\n\r\nprogram\r\n\r\n\tnum x;\r\n\tnum y;\r\n\tstring nome;\r\n\tbool ok;\r\n\r\n\tx  :=  0;\r\n\ty := 10;\r\n\tnome := \"Rodrigo\";\r\n\tok := true;\r\n\t\r\n\twhile( ok &eq true )begin\r\n\t\tif(  x &eq false )begin\r\n\t\t\tx  := x + 1;\r\n\t\tend\t\r\n\t\telse\r\n\t\tbegin\r\n\t\t\tok := false;\r\n\t\tend\t\r\n\tend\t\r\nendprog\r\n");
			fonte.setCharacterWidth(30);
			fonte.setVisibleLines(11);
			flexTable.setWidget(1, 0, fonte);
			flexTable.getCellFormatter().setHeight(1, 0, "");
			flexTable.getCellFormatter().setWidth(1, 0, "100%");
			fonte.setSize("100%", "");
		}
		{
			lblCdigoCompilado = new Label("Código Compilado");
			lblCdigoCompilado.setWordWrap(false);
			lblCdigoCompilado.setStyleName("gwt-MainPanel-Titulo");
			flexTable_1.setWidget(0, 0, lblCdigoCompilado);
		}
		{
			destino = new TextArea();
			destino.setStyleName("gwt-TesteTela-TextArea");
			destino.setCharacterWidth(30);
			destino.setVisibleLines(11);
			flexTable_1.setWidget(1, 0, destino);
			flexTable_1.getCellFormatter().setWidth(1, 0, "100%");
			destino.setWidth("100%");
		}
		{
			flexTable_2 = new FlexTable();
			flexTable_2.setSize("100%", "110px");
			flexTable_1.setWidget(2, 0, flexTable_2);
		}
		{
			espacoImg = new Image("off.png");
			flexTable_2.setWidget(0, 0, espacoImg);
		}
		{
			lblEspaosEmBranco = new Label("Espaços em branco");
			lblEspaosEmBranco.setWordWrap(false);
			flexTable_2.setWidget(0, 1, lblEspaosEmBranco);
		}
		{
			comentarioImg = new Image("off.png");
			flexTable_2.setWidget(1, 0, comentarioImg);
		}
		{
			lblComentrios = new Label("Comentários");
			lblComentrios.setWordWrap(false);
			flexTable_2.setWidget(1, 1, lblComentrios);
		}
		{
			tokensImg = new Image("off.png");
			flexTable_2.setWidget(2, 0, tokensImg);
		}
		{
			lblExtrairTokens = new Label("Extrair tokens");
			lblExtrairTokens.setWordWrap(false);
			flexTable_2.setWidget(2, 1, lblExtrairTokens);
		}
		FlexTableHelper.fixRowSpan(flexTable_2);
		{
			image = new Image("sobre.gif");
			image.setTitle("Saiba mais sobre essa aplicação");
			image.setStyleName("gwt-cursor-pointer");
			image.addClickHandler(new ClickHandler() {
				public void onClick(ClickEvent event) {
					History.newItem("sobre");
				}
			});
			{
				image_2 = new Image("logo.jpg");
				flexTable_4.setWidget(0, 0, image_2);
				flexTable_4.getCellFormatter().setWidth(0, 0, "100%");
			}
			flexTable_4.setWidget(0, 1, image);
			flexTable_4.getCellFormatter().setHeight(0, 1, "64px");
		}
		{
			image_1 = new Image("exec.png");
			image_1.setStyleName("gwt-cursor-pointer");
			image_1.setTitle("Clique aqui para testar.");
			image_1.addClickHandler(new ClickHandler() {
				public void onClick(ClickEvent event) {
					testar();
				}
			});
			flexTable.setWidget(2, 0, image_1);
			flexTable.getCellFormatter().setHeight(2, 0, "110px");
		}
		flexTable.getCellFormatter().setHorizontalAlignment(2, 0, HasHorizontalAlignment.ALIGN_RIGHT);
		flexTable.getCellFormatter().setVerticalAlignment(2, 0, HasVerticalAlignment.ALIGN_MIDDLE);
		flexTable_4.getCellFormatter().setHorizontalAlignment(0, 0, HasHorizontalAlignment.ALIGN_LEFT);
	}

	private void limpar(){
		destino.setText("");
		tokensImg.setUrl("off.png");
		comentarioImg.setUrl("off.png");
		espacoImg.setUrl("off.png");
	}

}
